/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2016年10月20日
 * <修改描述:>
 */
package com.tx.core.ddlutil.model;

import com.tx.core.ddlutil.DDLUtilsConstants;
import com.tx.core.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表定义<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2016年10月20日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class DBTableDef implements Serializable, TableDef {

    /**
     * 注释内容
     */
    private static final long serialVersionUID = 829927103002085200L;

    /**
     * 目录
     */
    private String catalog = null;

    /**
     * 数据库
     */
    private String schema = null;

    /**
     * 表名
     */
    private String name = null;

    /**
     * 表内容备注
     */
    private String comment = "";

    /**
     * 表类型：BASE TABLE|VIEW
     */
    private String type = DDLUtilsConstants.DDL_TABLE_TYPE_TABLE;

    /**
     * 主键集,主键是有顺序的
     */
    private List<DBTablePrimaryKey> primaryKeyList = new ArrayList<>();

    /**
     * 表字段.
     */
    private List<? extends TableColumnDef> columns = new ArrayList<>();

    /**
     * 索引集合.
     */
    private List<? extends TableIndexDef> indexes = new ArrayList<>();

    @Override
    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    @Override
    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String getPrimaryKey() {
        return this.primaryKeyList.stream().filter(p -> {
            return p != null;
        }).sorted((p1, p2) -> {
            return p1.getKeySeq().compareTo(p2.getKeySeq());
        }).map(pk -> pk.getColumnName()).collect(Collectors.joining(","));
    }

    public List<DBTablePrimaryKey> getPrimaryKeyList() {
        return primaryKeyList;
    }

    public void setPrimaryKeyList(List<DBTablePrimaryKey> primaryKeyList) {
        this.primaryKeyList = primaryKeyList;
    }

    @Override
    public List<? extends TableColumnDef> getColumns() {
        return columns;
    }

    public void setColumns(List<? extends TableColumnDef> columns) {
        this.columns = columns;
    }

    @Override
    public List<? extends TableIndexDef> getIndexes() {
        return indexes;
    }

    public void setIndexes(List<? extends TableIndexDef> indexes) {
        this.indexes = indexes;
    }

    /**
     * @return
     */
    @Override
    public int hashCode() {
        int hashCode = ObjectUtils.generateHashCode(super.hashCode(),
                this,
                "name");
        return hashCode;
    }

    /**
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        boolean flag = ObjectUtils.equals(this, obj, "name");
        return flag;
    }

    /**
     * <pre>
     * 功能简述: 表主键
     * </pre>
     * TABLE_CAT String => 表类别（可为 null）
     * TABLE_SCHEM String => 表模式（可为 null）
     * TABLE_NAME String => 表名称
     * COLUMN_NAME String => 列名称
     * KEY_SEQ short => 主键中的序列号
     * PK_NAME String => 主键的名称（可为 null）
     *
     * @author PengQingyang
     * 创建时间 [2024/3/16]
     */
    public static class DBTablePrimaryKey {

        private String name;

        private String tableName;

        private String columnName;

        private Integer keySeq = 0;

        public DBTablePrimaryKey() {
        }

        public DBTablePrimaryKey(String name, String columnName, Integer keySeq) {
            this.name = name;
            this.columnName = columnName;
            this.keySeq = keySeq == null ? 0 : keySeq;
        }

        public DBTablePrimaryKey(String tableName, String name, String columnName, Integer keySeq) {
            this.name = name;
            this.tableName = tableName;
            this.columnName = columnName;
            this.keySeq = keySeq == null ? 0 : keySeq;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTableName() {
            return tableName;
        }

        public void setTableName(String tableName) {
            this.tableName = tableName;
        }

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public Integer getKeySeq() {
            return keySeq;
        }

        public void setKeySeq(Integer keySeq) {
            this.keySeq = keySeq;
        }
    }
}
